package com.wmx.zookerper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.util.concurrent.CountDownLatch;

public class ProducerZookerper implements Watcher {
    private static CountDownLatch latch =  new CountDownLatch(1);
    private final static String path = "/runoob1";
    private static Stat stat = new Stat();
    public static void main(String[] args) throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper("192.168.199.128:2181,192.168.199.129:2181,192.168.199.130:2181", 4000, new ProducerZookerper());
        latch.await();
        System.out.println("zk connection");
        byte[]  temp = zooKeeper.getData(path,true,stat);
        System.out.println("init data :pulish node data"+new String(temp));
        int i=0;
        while(true){
            System.out.println( "publish new Data:"+i);
            Stat stat = zooKeeper.setData(path, String.valueOf(i).getBytes(), -1);
            int version = stat.getVersion();
            System.out.println("版本号"+version);
            Thread.sleep(5000L);
            i++;
        }
    }
    @Override
    public void process(WatchedEvent watchedEvent) {
        if(Event.KeeperState.SyncConnected == watchedEvent.getState()){
            System.out.println("receive watched event:"+watchedEvent);
            System.out.println(watchedEvent.getState());
            latch.countDown();
        }
    }
}
